//
//  CustomCellViewController.m
//  aTest
//
//  Created by 黄杰 on 15/11/5.
//  Copyright © 2015年 黄杰. All rights reserved.
//

static NSString *const kCustomCellID = @"kCustomCellID";

#import "CustomCellViewController.h"
#import "CustomCell.h"
#import "CustomGroup.h"
#import "UrlTestViewController.h"

@interface CustomCellViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSArray *customArray;
@property (nonatomic, strong) CustomGroup *group;
@end

@implementation CustomCellViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"自定义cell高度";
    
    [self setupTableView];
//    [self setupNetData];
    [self setupData];
    
}

/**
 *  设置TableView
 */
- (void)setupTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:kCustomCellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - UITableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.customArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:(kCustomCellID)];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCustomCellID];
    }
    
    cell.model = self.customArray[indexPath.section];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    UrlTestViewController *test = [[UrlTestViewController alloc] init];
    [self.navigationController pushViewController:test animated:YES];
}   

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return [tableView fd_heightForCellWithIdentifier:kCustomCellID configuration:^(CustomCell *cell) {
        cell.model = self.customArray[indexPath.section];
    }];
    
}

/**
 *  加载网络数据
 */
- (void)setupNetData
{
    self.page = 1;
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"access_token"] = @"0f96f631be8f64b7ed4c82e53dd41b9c";
    param[@"page"] = @(self.page);
    param[@"size"] = @"12";
    
    NSString *url = @"http://news.pts80.net/jyb/api/?action=message&control=message_list";
    
    [PTSManageTool PostWithURL:url parameters:param success:^(id json) {
        
        CustomGroup *group = [CustomGroup objectWithKeyValues:json];
        [group.list enumerateObjectsUsingBlock:^(CustomModel  *_Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
            model.content = [NSString stringWithFormat:@"%@dwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwdwd", model.content];
        }];
        self.group = group;
        self.customArray = [group.list copy];
        [self.tableView reloadData];
        
    } failure:^(PTSError *error) {
        NSLog(@"%@", error.errorString);
    }];
}

- (void)setupData
{
    self.customArray = [[CustomModel objectArrayWithFilename:@"Custom.plist"] copy];
    [self.tableView reloadData];
}

@end